
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";
const db = cloud.database()
const _ = db.command

export default async function (ctx: FunctionContext) {
  let body = ctx.body
  /**
   * {
   *   "_id": "weoojw",
   *   "roleName": "wuzuxian"
   * }
   */
  if (nw.util.isNull(body._id)){
    return fail("数据不合法")
  }
  if (nw.util.isNull(body.roleName)){
    return fail("数据不合法")
  }
  //2.修改的数据是否唯一
  let count = await nw.db.count({
    dbName:"role",
    whereJson:{
      roleName:_.eq(body.roleName),
      _id:_.neq(body._id),
      deleted:0
    }
  })
  if (count>0){
    return fail("角色名重复，无法修改")
  }
  //3.数据合法修改
  let num = await nw.db.update({
    dbName:"role",
    whereJson:{_id:_.eq(body._id)},
    dataJson:{roleName:body.roleName}
  })

  if (num===0){
    return fail("修改失败")
  }

  return ok("修改成功")

}
